package com.transpot.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.transpot.entry.exception.Exception;
import com.transpot.entry.order.Agreement;
import com.transpot.entry.order.Order;
import com.transpot.entry.order.OrderReturn;
import com.transpot.entry.order.OrderStatus;
import com.transpot.service.exception.ExceptionService;
import com.transpot.service.order.OrderReturnService;
import com.transpot.service.order.OrderService;
import com.transpot.service.order.OrderStatusService;
import com.transpot.utils.R;
import com.transpot.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/orderReturn")
public class OrderReturnController {
    @Autowired
    private OrderReturnService orderReturnService;
    @Autowired
    private OrderStatusService orderStatusService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ExceptionService exceptionService;

    /**
     * 上传回执单功能
     * @param multipartFile
     * @param Json
     * @return
     */
    @PostMapping("/addOrderReturn")
    public R addOrderReturn(@RequestParam("file") MultipartFile multipartFile, @RequestParam("form") String Json) {
        //调用自定义的图片保存方法
        String url = UploadUtil.uploadImg(multipartFile);
        //创建json转化对象
        ObjectMapper mapper = new ObjectMapper();
        //把json字符串转成对象
        OrderReturn orderReturn = null;
        try {
            orderReturn = mapper.readValue(Json, OrderReturn.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //设置图片访问的url
        orderReturn.setUrl(url);
        //设置状态为未处理
        orderReturn.setStatus(0);
        //调用mp的方法保存回执单信息
        orderReturnService.save(orderReturn);
        //修改订单状态为以上传回执单
        LambdaQueryWrapper<OrderStatus> orderStatusLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置订单状态对象的条件查询对象
        orderStatusLambdaQueryWrapper.eq(OrderStatus::getBaseId, orderReturn.getBaseId());
        //获取条件状态对象
        OrderStatus orderStatus = orderStatusService.getOne(orderStatusLambdaQueryWrapper);
        //设置状态为已上传回执单
        orderStatus.setStatus(4);
        //更新状态
        orderStatusService.updateById(orderStatus);
        return R.success();
    }

    /**
     * 获取全部未处理的回执单
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAllReturn")
    public R getAllReturn(int current, int size) {
        //创建回执单分页查询对象
        Page<OrderReturn> orderReturnPage = new Page<>(current, size);
        //创建回执单条件查询对象
        LambdaQueryWrapper<OrderReturn> orderReturnLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件为未处理的回执单
        orderReturnLambdaQueryWrapper.eq(OrderReturn::getStatus, 0);
        //执行分页查询
        orderReturnService.page(orderReturnPage, orderReturnLambdaQueryWrapper);
        //返回
        return R.success(orderReturnPage);
    }

    /**
     * 完成订单方法
     * @param orderReturn
     * @return
     */
    @PostMapping("/finish")
    public R finishOrder(@RequestBody OrderReturn orderReturn) {
        //进入service层去处理逻辑
        orderReturnService.finishOrder(orderReturn);
        //返回
        return R.success();
    }

    /**
     * 问题反馈方法
     * @param orderReturn
     * @return
     */
    @PostMapping("/question")
    public R updateReturn(@RequestBody OrderReturn orderReturn) {
        //设置订单状态为需要处理
        orderReturn.setStatus(2);
        orderReturnService.updateById(orderReturn);
        return R.success();
    }

    /**
     * 获取司机的全部回执单
     * @param current
     * @param size
     * @param id
     * @return
     */
    @GetMapping("/getReturnById")
    public R getReturnByDriverId(int current, int size, Integer id) {
        //创建回执单分页查询对象
        Page<OrderReturn> orderReturnPage = new Page<>(current, size);
        //创建条件查询对象
        LambdaQueryWrapper<OrderReturn> orderReturnLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置条件为与该司机主键一致
        orderReturnLambdaQueryWrapper.eq(OrderReturn::getDriverId, id);
        //执行查询
        orderReturnService.page(orderReturnPage, orderReturnLambdaQueryWrapper);
        return R.success(orderReturnPage);
    }

    /**
     * 删除回执单
     * @param orderReturn
     * @return
     */
    @PostMapping("/delReturn")
    public R delReturn(@RequestBody OrderReturn orderReturn) {
        //直接调用mp方法删除
        orderReturnService.removeById(orderReturn);
        return R.success();
    }
}
